package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.domain.WorkIssue;
import com.chen.dto.UserDto;
import com.chen.fegin.UserFegin;
import com.chen.mapper.WorkIssueMapper;
import com.chen.service.WorkIssueService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class WorkIssueServiceImpl extends ServiceImpl<WorkIssueMapper, WorkIssue> implements WorkIssueService {


    @Resource
    private UserFegin userFegin;

    @Override
    public Page<WorkIssue> findByPage(Page<WorkIssue> page, Integer status, String startTime, String endTime) {
        Page<WorkIssue> workIssuePage = page(page, new LambdaQueryWrapper<WorkIssue>()
                .eq(status != null, WorkIssue::getStatus, status)
                .between(
                        !StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime),
                        WorkIssue::getCreated,
                        startTime, endTime + " 23:59:59")
        );

        List<WorkIssue> records = workIssuePage.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return workIssuePage;
        }

        List<Long> ids = records.stream().map(WorkIssue::getUserId).collect(Collectors.toList());

        Map<Long, UserDto> user = userFegin.getUser(ids,null,null,null,null);
        for (WorkIssue workIssue : records) {
            UserDto userDto = user.get(workIssue.getUserId());
            workIssue.setUsername(userDto == null ? "匿名用户" : userDto.getUsername());
            workIssue.setRealName(userDto == null ? "匿名用户" : userDto.getRealName());
        }
        return workIssuePage;
    }
}
